package com.zyx.business.controller;

import com.zyx.business.domain.UserAddress;
import com.zyx.business.service.IUserAddressService;
import com.zyx.common.core.utils.poi.ExcelUtil;
import com.zyx.common.core.web.controller.BaseController;
import com.zyx.common.core.web.domain.AjaxResult;
import com.zyx.common.core.web.page.TableDataInfo;
import com.zyx.common.log.annotation.Log;
import com.zyx.common.log.enums.BusinessType;
import com.zyx.common.security.annotation.RequiresPermissions;
import com.zyx.common.security.utils.SecurityUtils;
import com.zyx.system.api.model.LoginUser;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户地址Controller
 *
 * @author zyx
 * @date 2025-09-25
 */
@RestController
@RequestMapping("/address")
public class UserAddressController extends BaseController {
    @Autowired
    private IUserAddressService userAddressService;

    /**
     * 查询用户地址列表
     */
    @RequiresPermissions("business:address:list")
    @GetMapping("/list")
    public TableDataInfo list(UserAddress userAddress) {
        //获取完整的登录用户对象 (LoginUser)
        LoginUser loginUser = SecurityUtils.getLoginUser();
        userAddress.setUserId(loginUser.getUserid());
        startPage();
        List<UserAddress> list = userAddressService.selectUserAddressList(userAddress);
        return getDataTable(list);
    }

    /**
     * 导出用户地址列表
     */
    @RequiresPermissions("business:address:export")
    @Log(title = "用户地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserAddress userAddress) {
        List<UserAddress> list = userAddressService.selectUserAddressList(userAddress);
        ExcelUtil<UserAddress> util = new ExcelUtil<UserAddress>(UserAddress.class);
        util.exportExcel(response, list, "用户地址数据");
    }

    /**
     * 获取用户地址详细信息
     */
    @RequiresPermissions("business:address:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(userAddressService.selectUserAddressById(id));
    }

    /**
     * 新增用户地址
     */
    @RequiresPermissions("business:address:add")
    @Log(title = "用户地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserAddress userAddress) {
        //获取完整的登录用户对象 (LoginUser)
        LoginUser loginUser = SecurityUtils.getLoginUser();
        userAddress.setUserId(loginUser.getUserid());
        return toAjax(userAddressService.insertUserAddress(userAddress));
    }

    /**
     * 修改用户地址
     */
    @RequiresPermissions("business:address:edit")
    @Log(title = "用户地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserAddress userAddress) {
        return toAjax(userAddressService.updateUserAddress(userAddress));
    }

    /**
     * 删除用户地址
     */
    @RequiresPermissions("business:address:remove")
    @Log(title = "用户地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userAddressService.deleteUserAddressByIds(ids));
    }
}
